package org.neuedu.chapter09_0629.demo06;

public class MyDemo01 {
    /**
     * getCounts
     * @param str 字符串
     * @return 返回 c 在 str 出现的次数
     */
    public int getCounts(String str,char c) {
        int count = 0;
        char[] chars = str.toCharArray();
        for (char aChar : chars) {
            if (aChar == c) {
                count++;
            }
        }
        return count;
    }

    /**
     *  getCounts 查单词
     * @param parent 父字符串
     * @param child 子串
     * @return 子串 在 父字符串 中出现的次数
     */
    public int getCounts(String parent,String child) {
        int count = 0;
        int index = parent.indexOf(child);
        while (index != -1) {
            count++;
            parent = parent.substring(index + child.length());
            index = parent.indexOf(child);
        }
        return count;
    }

    public static void main(String[] args) {
        MyDemo01 myDemo01 = new MyDemo01();
        int num = myDemo01.getCounts("aaaa", "aa");
        System.out.println(num);
    }
}
